﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WechatApiTest.Models;
using WechatApiTest.Utils;

namespace WechatApiTest.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WechatJSSDKController : ControllerBase
    {
        [HttpPost]
        public IActionResult Signature(RequestJSSignatureModel request)
        {
            try
            {
                // var jsTicket = await JsApiTicketContainer.TryGetJsApiTicketAsync("wxc5eda33a7a2f5fd3", "32cffc1af7396926f5a3c555116dd617");
                string appId = "wxc5eda33a7a2f5fd3";
                string appSecret = "32cffc1af7396926f5a3c555116dd617";
                ResponseWechatSignatureModel response = new()
                {
                    appId = "wxc5eda33a7a2f5fd3",
                    nonceStr = WechatHelper.CreatenNonceString(),
                    timestamp = WechatHelper.CreatenTimestamp(),

                };
                string signature = WechatHelper.GetSignature(WechatHelper.getJsTicket(WechatHelper.getTokenFromServer(appId, appSecret)), response.nonceStr, response.timestamp, request.url);
                response.signature = signature;
                return new JsonResult(new { code = 0, msg = "获取成功！", data = response });
            }
            catch (System.Exception e)
            {
                return new JsonResult(new { code = -1, msg = "出现异常了", data = e.Message });
            }
        }
    }
}
